package sicau.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import sicau.car.entity.Application;
import sicau.car.entity.User;
import sicau.car.mapper.UserMapper;
import sicau.car.result.R;
import sicau.car.service.ApplicationService;
import sicau.car.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ITcz
 * @since 2020-07-28
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    ApplicationService applicationService;

    @Override
    public IPage<Application> showOrder(int page, int pageSize, int userId) {
        return applicationService.showOwnOrder(page, pageSize, userId);
    }

    @Override
    public int getMonthlyFrequency(int userId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", userId);
        User user = userMapper.selectOne(queryWrapper);
        if (null == user){
            return -1;
        }
        return user.getMonthlyFrequency();
    }

    @Override
    public User getUser(String userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", userId);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public int insertUser(User user) {
        return userMapper.insert(user);
    }

    @Override
    public int addOne(Integer userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", userId);
        User user = userMapper.selectOne(queryWrapper);
        //用户预约次数加一
        int number = user.getMonthlyFrequency()+1;
        user.setMonthlyFrequency(number);
        return userMapper.update(user,queryWrapper);
    }

    @Override
    public Integer selectCount(String userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", userId);
        Integer number = userMapper.selectCount(queryWrapper);
        return number;
    }


}
